﻿#include "settingsdialog.h"
#include "ConfigDef.h"

#include <QFormLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRegularExpressionValidator>
#include <QSettings>
#include <QSpinBox>

SettingsDialog::SettingsDialog(QWidget *parent):QFrame(parent),_parent(parent) {
    setWindowFlags(Qt::Popup);
    this->resize(280,280);
    this->setObjectName("Settings-Dialog");

    this->setStyleSheet("SettingsDialog{"
                        "border:1px solid #E0E0E0;"
                        "background-color:#FFFFFF;"
                        "font-family:Microsoft YaHei;"
                        "font-size:15px;"
                        "font-weight:bold"
                        "}"
                        "QLineEdit{"
                        "border:1px solid #E0E0E0;"
                        "height:30px;"
                        "margin-bottom:12 px;"
                        "border-radius:5px;"
                        "}"
                        "QPushButton{"
                        "height:30px;"
                        "background-color:#1677FF;"
                        "border-radius:5px;"
                        "color:#FFFFFF;"
                        "margin-bottom:10px;"
                        "}"
                        "QPushButton:hover{"
                        "background-color:#008FFF;"
                        "}");
    auto layout = new QFormLayout(this);
    _serverAddrEdit = new QLineEdit(this);
    layout->addRow("服务器地址",_serverAddrEdit);

    _audioAddrEdit = new  QLineEdit(this);
    layout->addRow("音频地址",_audioAddrEdit);

    _audioPortEdit = new  QLineEdit(this);
    // QIntValidator *validator = new QIntValidator(0, 65535, _audioPortEdit);
    _audioPortEdit->setValidator(new QIntValidator(0, 65535, _audioPortEdit));
    layout->addRow("音频端口",_audioPortEdit);


    auto button = new QPushButton("确定",this);
    layout->setWidget(3,QFormLayout::SpanningRole,button);
    layout->setSpacing(5);
    layout->setContentsMargins(20,20,20,15);
    layout->setRowWrapPolicy(QFormLayout::WrapAllRows);
    connect(button,&QPushButton::clicked,this,&SettingsDialog::submit);
    this->setLayout(layout);
}

void SettingsDialog::showPopup(QPoint pos)
{
    auto config = QSettings(CONFIG_FILE,QSettings::IniFormat,this);
    _serverAddr = config.value("ServerAddress").toString();
    _audioAddr = config.value("AudioAddress").toString();
    _audioPort = config.value("AudioPort").toInt();
    _serverAddrEdit->setText(_serverAddr);
    _audioAddrEdit->setText(_audioAddr);
    _audioPortEdit->setText(QString::number(_audioPort));
    auto p =  _parent->mapToGlobal(QPoint(_parent->width() - this->width(),0));
    this->move(QPoint(p.x(),pos.y()));
    this->show();
}


void SettingsDialog::submit()
{
    auto config = QSettings(CONFIG_FILE,QSettings::IniFormat,this);
    auto serverAddr = _serverAddrEdit->text();
    auto audioAddr = _audioAddrEdit->text();
    auto audioPort = _audioPortEdit->text().toInt();
    config.setValue("ServerAddress",serverAddr);
    config.setValue("AudioAddress",audioAddr);
    config.setValue("AudioPort",audioPort);
    if(_serverAddr != serverAddr){//服务器地址改变->reload ui,stop media player
        emit serverChanged(serverAddr);
    }else if(audioAddr!=_audioAddr || audioPort!=_audioPort){//音频地址改变-> stop media player
        emit audioAddrChanged();
    }
    this->hide();
}
